package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.ProductDao;
import com.cn.wanxi.dto.ProductDto;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.util.JDBC;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductImpl implements ProductDao {
    @Override
    public List<ProductModel> getList() {
        String sql = "select * from tab_product order by id desc limit 2";
        ResultSet resultSet = JDBC.getResultSet(sql);
        List<ProductModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                ProductModel productModel = new ProductModel();
                productModel.setId(resultSet.getInt("id"));
                productModel.setTitle(resultSet.getString("title"));
                productModel.setImgHref(resultSet.getString("img_href"));
                productModel.setContent(resultSet.getString("content"));
                list.add(productModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<ProductDto> findAll() {
        String sql = "SELECT a.* ,b.name FROM tab_product a,tab_cuisine b " +
                " where a.cuisine_id=b.id " +
                " order by id desc";
        ResultSet resultSet = JDBC.getResultSet(sql);
        List<ProductDto> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                ProductDto productDto = new ProductDto();
                productDto.setId(resultSet.getInt("id"));
                productDto.setTitle(resultSet.getString("title"));
                productDto.setImgHref(resultSet.getString("img_href"));
                productDto.setContent(resultSet.getString("content"));
                String price = resultSet.getString("price");
                productDto.setPrice(price == null ? -1 : Double.parseDouble(price));
                productDto.setCuisineId(resultSet.getInt("cuisine_id"));
                Date date = resultSet.getDate("create_time");
                productDto.setCreateTime(date == null ? "" : date.toString());
                productDto.setName(resultSet.getString("name"));
                list.add(productDto);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int add(ProductModel productModel) {
        String sql = "insert into tab_product (title,content,price,cuisine_id,img_href,create_time)" +
                " values ('" + productModel.getTitle() + "'," +
                " '" + productModel.getContent() + "'," + productModel.getPrice() + "," +
                " " + productModel.getCuisineId() + ",'" + productModel.getImgHref() + "'," +
                "'" + productModel.getCreateTime() + "')";
        return JDBC.excuteUpdate(sql);
    }

    @Override
    public int del(Integer id) {
        String sql = "delete from tab_product where id=" + id;
        return JDBC.excuteUpdate(sql);
    }

    @Override
    public ProductDto findById(Integer id) {
        String sql = "SELECT a.* ,b.name FROM tab_product a,tab_cuisine b " +
                " where a.cuisine_id=b.id " +
                " and a.id=" + id;
        ResultSet resultSet = JDBC.getResultSet(sql);
        ProductDto productDto = null;
        try {
            while (resultSet.next()) {
                productDto = new ProductDto();
                productDto.setId(resultSet.getInt("id"));
                productDto.setTitle(resultSet.getString("title"));
                productDto.setImgHref(resultSet.getString("img_href"));
                productDto.setContent(resultSet.getString("content"));
                String price = resultSet.getString("price");
                productDto.setPrice(price == null ? -1 : Double.parseDouble(price));
                productDto.setCuisineId(resultSet.getInt("cuisine_id"));
                Date date = resultSet.getDate("create_time");
                productDto.setCreateTime(date == null ? "" : date.toString());
                productDto.setName(resultSet.getString("name"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return productDto;
    }

    @Override
    public int update(ProductModel productModel) {
        String sql = "update tab_product set title='" + productModel.getTitle() + "'," +
                " content='" + productModel.getContent() + "'," +
                " price='" + productModel.getPrice() + "'," +
                " cuisine_id='" + productModel.getCuisineId() + "'," +
                " img_href='" + productModel.getImgHref() + "'," +
                " create_time='" + productModel.getCreateTime() + "' " +
                " where id=" + productModel.getId();
        return JDBC.excuteUpdate(sql);
    }
}
